﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    int count = 1, k = 0;
    Image[,] array = new Image[20, 20];//20*20矩阵
    #region 元素图片管理
    //[System.Serializable]
    //public struct CellPic
    //{
    //    public Sprite zero;
    //    public Sprite one;
    //    public Sprite two;
    //    public Sprite three;
    //    public Sprite four;
    //    public Sprite five;
    //    public Sprite six;
    //    public Sprite seven;
    //    public Sprite eight;
    //    public Sprite def;
    //    public Sprite mine;
    //}
    //public CellPic cellPic;//元素图片管理
    public List<Sprite> cellList;
    public Sprite def;
    public Sprite mine;
    #endregion

    List<Image> list_mine = new List<Image>();//雷集合

    public static Main _instance;
    private void Awake()
    {
        _instance = this;
    }
    void Start()
    {
        GameObject game = transform.Find("Image").gameObject;
        game.name = "cell";

        while (count < 400)
        {
            GameObject clone = Instantiate(game, transform);
            clone.name = game.name;
            count++;
        }

        for (int i = 0; i < 20; i++)
        {
            for (int j = 0; j < 20; j++, k++)
            {
                array[i, j] = transform.GetChild(k).GetComponent<Image>();
                array[i, j].gameObject.AddComponent<Element>();
            }
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            UncoverMines();
            Invoke("HideMine", 1);
        }
    }

    /// <summary>
    /// 布雷
    /// </summary>
    void InitMine(int num)
    {
        if (num <= 0)
        {
            return;//雷已布完，请客官来踩
            //Invoke("HideMine",1);//给你看一眼
        }
        else
        {
            int a = Random.Range(0, 20);
            int b = Random.Range(0, 20);
            if (array[a, b].name == "mine")
            {
                InitMine(num);//重新随机
            }
            else
            {
                array[a, b].sprite = mine;
                array[a, b].name = "mine";
                list_mine.Add(array[a, b]);
                num--;//随机下一个
                InitMine(num);
            }
        }
    }
    /// <summary>
    /// 藏雷
    /// </summary>
    void HideMine()
    {
        foreach (var item in list_mine)
        {
            item.sprite = def;
        }
    }
    /// <summary>
    /// 踩着雷后全部显示
    /// </summary>
    public void UncoverMines()
    {
        foreach (var item in array)
        {
            if (item.GetComponent<Element>().mine)
            {
                item.sprite = mine;
                list_mine.Add(item);//方便开启外挂模式
            }
        }
    }
    /// <summary>
    /// 获取点击图片的索引
    /// </summary>
    public void GetPosition(Image image)
    {
        int a = 0, b = 0;
        for (int i = 0; i < 20; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                if (array[i, j] == image)
                {
                    a = i;
                    b = j;
                    //print("点击" + "(" + a + "," + b + ")");
                    ChackAround(a, b);

                    FloodFill(a, b, new bool[20, 20]);
                }
            }
        }
    }
    /// <summary>
    /// 判断点击的合法性
    /// </summary>
    bool MineAt(int x,int y)
    {
        if (x >= 0 && y >= 0 && x < 19 && y < 19)
        {
            return array[x, y].GetComponent<Element>().mine;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 检查周围雷的数目
    /// </summary>
    int ChackAround(int a, int b)
    {
        int c = 0;

        for (int i = -1; i < 2; i++)
        {
            for (int j = -1; j < 2; j++)
            {
                if (MineAt(a+i,b+j))
                {
                    ++c;
                }
            }
        }
        return c;
        //image.sprite = cellList[c];
    }
    /// <summary>
    /// 点击的周围没有雷，区域显示
    /// </summary>
    void FloodFill(int x, int y, bool[,] visited)
    {
        //print("x:" + x + "," + "y:" + y);
        if (x >= 0 && y >= 0 && x < 20 && y < 20)
        {
            if (visited[x, y]) return;

            array[x, y].sprite = cellList[ChackAround(x, y)];

            if (ChackAround(x, y) > 0) return;

            visited[x, y] = true;

            FloodFill(x - 1, y, visited);
            FloodFill(x + 1, y, visited);
            FloodFill(x, y - 1, visited);
            FloodFill(x, y + 1, visited);
        }
    }
}
